package com.gears_software.bbnetwork.activitys;

import java.util.ArrayList;
import java.util.List;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.WindowFeature;
import org.androidannotations.annotations.sharedpreferences.Pref;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVAnalytics;
import com.gears_software.bbnetwork.R;
import com.gears_software.bbnetwork.adapters.ViewpagerAdapter;
import com.gears_software.bbnetwork.datas.finders.IBBNetworkFinder;
import com.gears_software.bbnetwork.datas.finders.impl.IBBNetworkFinderImpl;
import com.gears_software.bbnetwork.datas.models.MainModel;
import com.gears_software.bbnetwork.datas.models.RankingInfosModel;
import com.gears_software.bbnetwork.datas.models.ResultModel;
import com.gears_software.bbnetwork.datas.models.ReturnCode;
import com.gears_software.bbnetwork.datas.models.ViwepagerModel;
import com.gears_software.bbnetwork.dialogs.StyleDialog;
import com.gears_software.bbnetwork.dialogs.StyleDialog.IStyleDialogResult;
import com.gears_software.bbnetwork.utlis.ActivityUtils;
import com.gears_software.bbnetwork.utlis.BBNetworkSharedPref_;
import com.gears_software.bbnetwork.utlis.ToastUI;
import com.gears_software.common.trinea.utils.ListUtils;
import com.gears_software.common.ui.scrollviewpager.AutoScrollViewPager;

/**
 * 第二版本主页
 * 
 * @author DY-PC
 * 
 */
@EActivity(R.layout.activity_main2)
@WindowFeature(Window.FEATURE_NO_TITLE)
public class Main2Activity extends Activity {

	@ViewById
	AutoScrollViewPager autoViewPager;// 图片轮播

	@ViewById
	LinearLayout gallery_point_linear;// 轮播图片小点

	@ViewById
	LinearLayout image_layout;

	@ViewById
	LinearLayout image_layout2;

	@ViewById
	TextView txtName1;
	@ViewById
	TextView txtName2;
	@ViewById
	TextView txtName3;
	@ViewById
	TextView txtName4;
	@ViewById
	TextView txtName5;

	@ViewById
	TextView txtRmb1;
	@ViewById
	TextView txtRmb2;
	@ViewById
	TextView txtRmb3;
	@ViewById
	TextView txtRmb4;
	@ViewById
	TextView txtRmb5;

	@Pref
	BBNetworkSharedPref_ sp;

	@Bean(IBBNetworkFinderImpl.class)
	IBBNetworkFinder finder;

	List<ViwepagerModel> list;// 轮播图片

	@Bean
	ToastUI toast;

	ViewpagerAdapter pagerAdapter;

	int image[] = { R.drawable.bb_number_black_0, R.drawable.bb_number_black_1,
			R.drawable.bb_number_black_2, R.drawable.bb_number_black_3,
			R.drawable.bb_number_black_4, R.drawable.bb_number_black_5,
			R.drawable.bb_number_black_6, R.drawable.bb_number_black_7,
			R.drawable.bb_number_black_8, R.drawable.bb_number_black_9 };
	int image2[] = { R.drawable.bb_number_red_0, R.drawable.bb_number_red_1,
			R.drawable.bb_number_red_2, R.drawable.bb_number_red_3,
			R.drawable.bb_number_red_4, R.drawable.bb_number_red_5,
			R.drawable.bb_number_red_6, R.drawable.bb_number_red_7,
			R.drawable.bb_number_red_8, R.drawable.bb_number_red_9 };

	@AfterViews
	void onCreate() {
		ActivityUtils.listActivity.add(this);
		getData();
	}

	@Background
	void getData() {
		ResultModel<MainModel> res = finder.getHomeData();
		if (res.getReturnCode().equals(ReturnCode.SUCCEED)) {
			initView(res.getResObj());
		} else {
			showTxt(res.getMessage());
		}
	}

	@UiThread
	void initView(MainModel model) {
		if (model != null && !model.toString().equals("")) {
			initViewpager(model.getAppActivityInfoGroup());
			initRankingInfos(model.getWithdrawalsRankingInfos());
			initXiaoliang(model);
		}
	}

	void initXiaoliang(MainModel model) {
		String baodanCount = model.getProjectsNo();
		String chengjiaoCount = model.getTransactionsNo();
		List<Integer> baodanList = StrOrInt(baodanCount);
		List<Integer> chengjiaoList = StrOrInt(chengjiaoCount);
		if (baodanList != null) {
			for (Integer integer : baodanList) {
				ImageView images = new ImageView(this);
				if (integer == 0) {
					images.setImageResource(image[0]);
					image_layout.addView(images);
				}
				if (integer == 1) {
					images.setImageResource(image[1]);
					image_layout.addView(images);
				}
				if (integer == 2) {
					images.setImageResource(image[2]);
					image_layout.addView(images);
				}
				if (integer == 3) {
					images.setImageResource(image[3]);
					image_layout.addView(images);
				}
				if (integer == 4) {
					images.setImageResource(image[4]);
					image_layout.addView(images);
				}
				if (integer == 5) {
					images.setImageResource(image[5]);
					image_layout.addView(images);
				}
				if (integer == 6) {
					images.setImageResource(image[6]);
					image_layout.addView(images);
				}
				if (integer == 7) {
					images.setImageResource(image[7]);
					image_layout.addView(images);
				}
				if (integer == 8) {
					images.setImageResource(image[8]);
					image_layout.addView(images);
				}
				if (integer == 9) {
					images.setImageResource(image[9]);
					image_layout.addView(images);
				}
			}
		}

		if (chengjiaoList != null) {
			for (Integer integer : chengjiaoList) {
				ImageView images = new ImageView(this);
				if (integer == 0) {
					images.setImageResource(image2[0]);
					image_layout2.addView(images);
				}
				if (integer == 1) {
					images.setImageResource(image2[1]);
					image_layout2.addView(images);
				}
				if (integer == 2) {
					images.setImageResource(image2[2]);
					image_layout2.addView(images);
				}
				if (integer == 3) {
					images.setImageResource(image2[3]);
					image_layout2.addView(images);
				}
				if (integer == 4) {
					images.setImageResource(image2[4]);
					image_layout2.addView(images);
				}
				if (integer == 5) {
					images.setImageResource(image2[5]);
					image_layout2.addView(images);
				}
				if (integer == 6) {
					images.setImageResource(image2[6]);
					image_layout2.addView(images);
				}
				if (integer == 7) {
					images.setImageResource(image2[7]);
					image_layout2.addView(images);
				}
				if (integer == 8) {
					images.setImageResource(image2[8]);
					image_layout2.addView(images);
				}
				if (integer == 9) {
					images.setImageResource(image2[9]);
					image_layout2.addView(images);
				}

			}
		}
	}

	// 将字符串类型的数据，截取为int类型的集合
	private List<Integer> StrOrInt(String str) {
		if (str == null) {
			return null;
		}
		List<Integer> list = new ArrayList<Integer>();
		int start = 0;
		int end = start + 1;
		while (true) {
			if (start >= str.length()) {
				return list;
			}
			String temp = str.substring(start, end);
			Integer integer = Integer.valueOf(temp);
			list.add(integer);
			start = end;
			end = end + 1;
			if (end >= str.length()) {
				end = str.length();
			}
		}
	}

	void initRankingInfos(List<RankingInfosModel> list) {
		if (list != null && list.size() > 0) {
			for (RankingInfosModel model : list) {
				if (model.getOrder().equals("1")) {
					txtName1.setText(model.getName());
					txtRmb1.setText(model.getAmount());
				}
				if (model.getOrder().equals("2")) {
					txtName2.setText(model.getName());
					txtRmb2.setText(model.getAmount());
				}
				if (model.getOrder().equals("3")) {
					txtName3.setText(model.getName());
					txtRmb3.setText(model.getAmount());
				}
				if (model.getOrder().equals("4")) {
					txtName4.setText(model.getName());
					txtRmb4.setText(model.getAmount());
				}
				if (model.getOrder().equals("5")) {
					txtName5.setText(model.getName());
					txtRmb5.setText(model.getAmount());
				}
			}
		}
	}

	void initViewpager(List<ViwepagerModel> pagerList) {
		this.list = pagerList;
		if (list != null) {
			// 添加滑动事件，自定义事件
			autoViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
			initIcon();
			pagerAdapter = new ViewpagerAdapter(list, this);
			autoViewPager.setAdapter(pagerAdapter);
			autoViewPager.setInterval(2000);// 设置多长时间
			autoViewPager.setCurrentItem(Integer.MAX_VALUE / 2
					- Integer.MAX_VALUE / 2 % ListUtils.getSize(list));
			autoViewPager.startAutoScroll();
		}

	}

	// 自定义滑动事件， 用于记录当前滑动第几页
	public class MyOnPageChangeListener implements OnPageChangeListener {
		
		@Override
		public void onPageSelected(int position) {
			// indexText.setText(new StringBuilder().append((position) %
			// ListUtils.getSize(imageIdList) + 1).append("/")
			// .append(ListUtils.getSize(imageIdList)));
			scrollView(position);
		}

		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}
	}

	public void scrollView(int position) {
		View view = gallery_point_linear.getChildAt(((position) % ListUtils.getSize(list) - 1) == -1 ? list.size() - 1
				: (position) % ListUtils.getSize(list) - 1);
		View view2 = gallery_point_linear.getChildAt(position);
		View view3 = gallery_point_linear.getChildAt(((position)
				% ListUtils.getSize(list) + 1) > list.size() - 1 ? 0
				: (position) % ListUtils.getSize(list) + 1);
		ImageView img1 = (ImageView) view;
		ImageView img2 = (ImageView) view2;
		ImageView img3 = (ImageView) view3;
		img1.setBackgroundResource(R.drawable.feature_point);
		img2.setBackgroundResource(R.drawable.feature_point_cur);
		img3.setBackgroundResource(R.drawable.feature_point);
	}

	// 提现
	@Click
	void relative_tixian() {
		Intent intent = new Intent(this, TiXianActivity_.class);
		startActivity(intent);
	}

	// 我的点击事件
	@Click
	void layout_my() {
		Intent i = new Intent(this, MyActivity_.class);
		startActivity(i);
	}

	// 新增
	@Click
	void image_add() {
		String status = sp.status().get();
		if (status.equals("2")) {
			Intent i = new Intent(this, BaoDanActivity_.class);
			startActivity(i);
		} else {
			final StyleDialog dialog = new StyleDialog(this, R.style.MyDialog);
			dialog.setOnIstyleDialogResult(new IStyleDialogResult() {
				@Override
				public void returnCode(int type) {
					if (type == 0) {
						dialog.dismiss();
					}
					if (type == 1) {
						Intent i = new Intent(Main2Activity.this,
								MyInfoActivity_.class);
						startActivity(i);
						dialog.dismiss();
					}
				}
			});
			dialog.show();
		}
	}

	// 初始化小圆点
	private void initIcon() {
		for (int i = 0; i < list.size(); i++) {
			ImageView pointView = new ImageView(this);
			if (i == 0) {
				pointView.setBackgroundResource(R.drawable.feature_point_cur);
			} else {
				pointView.setBackgroundResource(R.drawable.feature_point);
			}
			gallery_point_linear.addView(pointView);
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		AVAnalytics.onResume(this);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		AVAnalytics.onPause(this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		ActivityUtils.listActivity.remove(this);
	}

	@UiThread
	void showTxt(String txt) {
		toast.ShowToast(txt);
	}

	private long mExitTime;

	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if ((System.currentTimeMillis() - mExitTime) > 2000) {
				Object mHelperUtils;
				Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
				mExitTime = System.currentTimeMillis();
			} else {
				finish();
			}
			return true;
		}

		return super.onKeyDown(keyCode, event);

	}
}
